<?php
/*
 * @Author: your name
 * @Date: 2020-10-13 17:25:46
 * @LastEditTime: 2020-12-28 14:07:44
 * @LastEditors: Please set LastEditors
 * @Description: 主机访问日志管理接口
 * @FilePath: /special/application/adminapiv3/controller/module_manage/netfilter/AccessLogController.php
 */

namespace app\adminapiv3\controller\module_manage\netfilter;

use think\facade\Request;
use app\adminapiv3\service\module_manage\netfilter\AccessLogService;

class AccessLogController extends NetfilterBaseController
{
    /**
     * @name 主机日志列表
     * @param $host_id
     * @param $ignore
     * @author wx
     * @date 2019/10/31 14:27
     */
    public function list($host_id, $ignore)
    {
        $input = Request::only(['source_ip', 'target_ip'],'get');
        $input['ignore'] = $ignore;
        $this->validate($input, 'AccessLog.ipGroupListFilter');//验证数据

        $AccessLogService = new AccessLogService();
        $data = $AccessLogService->list($host_id, $input);

        return $this->jsonOutPut($data);
    }

    /**
     * @name 详情
     * @param $host_id
     * @param $ignore
     * @author wx
     * @date 2019/10/31 16:59
     */
    public function detail($host_id, $ignore)
    {
        $input = Request::only(['source_ip','direction','target_ip','target_port','protocols','permission','first_time','last_time'], 'get');
        $input['ignore'] = $ignore;

        $this->validate($input, 'AccessLog.ipGroupDetailFilter');//验证数据
        $AccessLogService = new AccessLogService();
        $data = $AccessLogService->detail($host_id, $input);

        return $this->jsonOutPut($data);
    }

    /***
     * @name 开启日志记录
     * @param $host_id
     * @author wx
     * @date 2019/11/1 9:59
     */
    public function open($host_id)
    {
        $AccessLogService = new AccessLogService();
        $AccessLogService->logReportOn($host_id);

        //下发命令 防火墙日志状态
        $this->sendCommandToClient('netfilter','FirewallLogStatus');
        return $this->jsonOutPut([], true);
    }

    /**
     * @name 关闭日志记录
     * @param $host_id
     * @author wx
     * @date 2019/11/1 10:22
     */
    public function close($host_id) 
    {
        $AccessLogService = new AccessLogService();
        $AccessLogService->logReportOff($host_id);

        //下发命令 防火墙日志状态
        $this->sendCommandToClient('netfilter','FirewallLogStatus');
        return $this->jsonOutPut([], true);
    }

    /**
     * @name 读取日志选项
     * @param $host_id
     * @author wx
     * @date 2019/11/1 13:12
     */
    public function readConfig($host_id)
    {
        $AccessLogService = new AccessLogService();
        $data = $AccessLogService->readConfig($host_id);

        return $this->jsonOutPut($data);
    }

    /**
     * @name 修改日志选项
     * @author wx
     * @date 2019/11/1 13:12
     */
    public function modifyConfig()
    {
        $input = Request::only(['host_id','expired_time','frequency','process'], 'post');
        $this->validate($input, 'AccessLog.modifyConfig');

        $AccessLogService = new AccessLogService();
        $AccessLogService->modifyConfig($input);

        //下发命令 防火墙日志状态
        $this->sendCommandToClient('netfilter','FirewallLogStatus');
        return $this->jsonOutPut([], true);
    }

    /**
     * @name 忽略日志
     * @param $host_id
     * @author wx
     * @date 2019/11/1 13:12
     */

    public function ignoreLog($host_id)
    {
        $input = Request::only(['id'], 'post');
        $this->validate($input, 'AccessLog.ignoreLog');
        
        $AccessLogService = new AccessLogService();
        $AccessLogService->ignoreLog($host_id, $input['id']);

        return $this->jsonOutPut();
    }

    /**
     * @name 取消忽略日志
     * @param $host_id
     * @author wx
     * @date 2019/11/1 13:12
     */
    public function unignoreLog($host_id)
    {
        $input = Request::only(['id'], 'post');
        $this->validate($input, 'AccessLog.ignoreLog');

        $AccessLogService = new AccessLogService();
        $AccessLogService->unignoreLog($host_id, $input['id']);

        return $this->jsonOutPut();
    }
    
    /**
     * @name: 警告列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-09 19:17:31
     */
    public function warringList ($host_id)
    {
        $input = Request::only(['target_ip','source_ip','direction','protocols','permission','target_port'], 'post');

        $this->validate($input, 'AccessLog.warringListFilter');

        $input['host_id'] = $host_id;//主机id

        $AccessLogService = new AccessLogService();
        $data = $AccessLogService->warringList($input);
       

        return $this->jsonOutPut($data);
    }
}